<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2019-03-04
 * Time: 14:43
 */

namespace Biz\module\Classes;


use Biz\model\salesRebate\SalesCoupon as mSalesCoupon;
use Doraemon\tools\IdParse;
use Biz\model\activity\CouponConfig as mCouponConfig;
use Biz\service\activity\Coupon as sCoupon;

class CouponRebate extends Coupon
{
    /**
     * 生成一个设计师返利的优惠码
     */
    public static function genRebateCouponCode($uid, $pre = '', $card = '')
    {
        $hashUid = IdParse::hashUid($uid);

        if(!empty($pre)) {
            if(isset($card['card_belongs'])){
                if($card['card_belongs'] == 1){
                    $tpCode = sprintf($pre . "P%s" . $card['pre_num'], $hashUid);
                    $tcCode = '';
                }else if($card['card_belongs'] == 2){
                    $tcCode = sprintf($pre . "C%s" . $card['pre_num'], $hashUid);
                    $tpCode = '';
                }
            }else {
                $tcCode = sprintf($pre . "C%s", $hashUid);
                $tpCode = sprintf($pre . "P%s", $hashUid);
            }
        }else{
            $tcCode = sprintf("TCX%s", $hashUid);
            $tpCode = sprintf("TPX%s", $hashUid);
        }

        return [
            'tc' => $tcCode,
            'tp' => $tpCode
        ];
    }

    /*
     * 创建一个返利账户到数据库中，如果存在，则返回返利账户的信息
     */
    public static function createSalesRebateCoupon($uid, $params = '', $is_create = true, $coupon = '')
    {
        $info = mSalesCoupon::getInfo($uid);

        if (!empty($info) && $is_create) {
            return $info;
        }

        if(!empty($params) && $params['belongs']>0) {
            $genCodes = self::genRebateCouponCode($uid, mCouponConfig::$belongs_en[$params['belongs']], $params);
        }else{
            $genCodes = self::genRebateCouponCode($uid);
        }

        $couponCodes = array_filter(array_values($genCodes));

        $couponInfos = mCouponConfig::getByCouponCodes($couponCodes);

        if (!empty($couponInfos)) {
            throw new \Exception('优惠码已存在');
        }

        //创建设计师优惠码
        $couponList = mCouponConfig::createRebateCoupon($couponCodes, $is_create, $params, $coupon);

        if (!empty($couponList)) {
            foreach ($couponList as $id) {
                sCoupon::cacheCouponConfig($id);
            }
        }

        if($is_create) {
            $result = mSalesCoupon::createOne(
                $uid,
                [
                    'code' => $genCodes['tc'],
                    'id' => !empty($genCodes['tc']) ? $couponList[$genCodes['tc']] : 0,
                ],
                [
                    'code' => $genCodes['tp'],
                    'id' => !empty($genCodes['tp']) ? $couponList[$genCodes['tp']] : 0,
                ],
                $params
            );
            return $result;
        }else{
            if(!empty($genCodes['tc'])){
                return [
                    'code' => $genCodes['tc'],
                    'id' => !empty($genCodes['tc']) ? $couponList[$genCodes['tc']] : 0,
                ];
            }else{
                return [
                    'code' => $genCodes['tp'],
                    'id' => !empty($genCodes['tp']) ? $couponList[$genCodes['tp']] : 0,
                ];
            }
        }

    }

    public static function disableSalesRebateCoupon($tpCouponId = '', $tcCouponId = '', $coupon_id = '')
    {
        $couponIds = [];
        if (!empty($tpCouponId)) {
            $couponIds[] = $tpCouponId;
        }
        if (!empty($tcCouponId)) {
            $couponIds[] = $tcCouponId;
        }
        if(!empty($coupon_id)){
            $couponIds = array_merge($couponIds, $coupon_id);
        }

        $ret = mCouponConfig::update([
            'status' => mCouponConfig::STATUS_CAN_NOT_RECEIVE
        ], [
            'coupon_id' => $couponIds
        ]);

        if (!empty($ret) && $ret->rowCount() > 0) {
            foreach ($couponIds as $id) {
                sCoupon::cacheCouponConfig($id);
            }
            return true;
        }
        return false;
    }

    public static function enableSalesRebateCoupon($tpCouponId = '', $tcCouponId = '', $coupon_id = '')
    {
        $couponIds = [];
        if (!empty($tpCouponId)) {
            $couponIds[] = $tpCouponId;
        }
        if (!empty($tcCouponId)) {
            $couponIds[] = $tcCouponId;
        }
        if(!empty($coupon_id)){
            $couponIds = array_merge($couponIds, $coupon_id);
        }
        $ret = mCouponConfig::update([
            'status' => mCouponConfig::STATUS_CAN_RECEIVE
        ], [
            'coupon_id' => $couponIds
        ]);

        if (!empty($ret) && $ret->rowCount() > 0) {
            foreach ($couponIds as $id) {
                sCoupon::cacheCouponConfig($id);
            }
            return true;
        }
        return false;
    }


}